package com.m1905.lib.download.model;


import android.os.Parcel;
import android.os.Parcelable;

import com.m1905.lib.download.task.DownloadTask;


/**
 * @author wanghao
 * @date 2022/8/16.
 * @des
 */
public class DownloadDBItem implements Parcelable {

    private long id;
    private String title;
    private String contentId;
    private String episodeId;
    private String episode;
    private String url;
    private int isVip;
    private String downloadId;
    private int downloadType;// 1 电影 2电视剧
    private int type;//内容类型type 2免费 7付费
    private int definition; //清晰度: SD(1, "标清"), HD(2, "高清"), FHD(3, "超清"), UHD(4, "超高清");
    private int bmonth;//付费内容类型 0（既可包月又可单点） 1仅单点 2仅包月
    private String urlRouter;
    private String img;
    private String path;
    private boolean resume = true;
    private boolean autoRename = true;
    private @State int state;
    private long totalSize;
    private long downloadSize;
    private long downloadSpeed;
    private int errorCode;
    private String errorMsg;
    private long watchTime;
    private long createTime;
    private DownloadTask task;


    public DownloadDBItem() {
//        DLog.e("DownloadDBItem.DownloadDBItem null :");
    }

    public DownloadDBItem(String title, String contentId, String episodeId, String episode, String url, int isVip, String downloadId, int downloadType, int type, int definition, int bmonth, String urlRouter, String img) {
        this.title = title;
        this.contentId = contentId;
        this.episodeId = episodeId;
        this.episode = episode;
        this.url = url;
        this.isVip = isVip;
        this.downloadId = downloadId;
        this.downloadType = downloadType;
        this.type = type;
        this.definition = definition;
        this.bmonth = bmonth;
        this.urlRouter = urlRouter;
        this.img = img;
//        DLog.e("DownloadDBItem.DownloadDBItem args :");
    }

    public DownloadDBItem(Parcel in) {
        readFromParcel(in);
//        DLog.e("DownloadDBItem.DownloadDBItem Parcel :");
    }


    public static final Creator<DownloadDBItem> CREATOR = new Creator<DownloadDBItem>() {
        @Override
        public DownloadDBItem createFromParcel(Parcel in) {
            return new DownloadDBItem(in);
        }

        @Override
        public DownloadDBItem[] newArray(int size) {
            return new DownloadDBItem[size];
        }
    };

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContentId() {
        return contentId;
    }

    public void setContentId(String contentId) {
        this.contentId = contentId;
    }

    public String getEpisodeId() {
        return episodeId;
    }

    public void setEpisodeId(String episodeId) {
        this.episodeId = episodeId;
    }

    public String getEpisode() {
        return episode;
    }

    public void setEpisode(String episode) {
        this.episode = episode;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getIsVip() {
        return isVip;
    }

    public void setIsVip(int isVip) {
        this.isVip = isVip;
    }

    public String getDownloadId() {
        return downloadId;
    }

    public void setDownloadId(String downloadId) {
        this.downloadId = downloadId;
    }

    public int getDownloadType() {
        return downloadType;
    }

    public void setDownloadType(int downloadType) {
        this.downloadType = downloadType;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getDefinition() {
        return definition;
    }

    public void setDefinition(int definition) {
        this.definition = definition;
    }

    public int getBmonth() {
        return bmonth;
    }

    public void setBmonth(int bmonth) {
        this.bmonth = bmonth;
    }

    public String getUrlRouter() {
        return urlRouter;
    }

    public void setUrlRouter(String urlRouter) {
        this.urlRouter = urlRouter;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public boolean isResume() {
        return resume;
    }

    public void setResume(boolean resume) {
        this.resume = resume;
    }

    public boolean isAutoRename() {
        return autoRename;
    }

    public void setAutoRename(boolean autoRename) {
        this.autoRename = autoRename;
    }

    public @State int getState() {
        return state;
    }

    public void setState(@State int state) {
        this.state = state;
    }

    public long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }

    public long getDownloadSize() {
        return downloadSize;
    }

    public void setDownloadSize(long downloadSize) {
        this.downloadSize = downloadSize;
    }

    public long getDownloadSpeed() {
        return downloadSpeed;
    }

    public void setDownloadSpeed(long downloadSpeed) {
        this.downloadSpeed = downloadSpeed;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public long getWatchTime() {
        return watchTime;
    }

    public void setWatchTime(long watchTime) {
        this.watchTime = watchTime;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public DownloadTask getTask() {
        return task;
    }

    public void setTask(DownloadTask task) {
        this.task = task;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DownloadDBItem that = (DownloadDBItem) o;

        if (downloadType != that.downloadType) return false;
        if (type != that.type) return false;
        if (bmonth != that.bmonth) return false;
        if (!equals(contentId, that.contentId))
            return false;
        if (!equals(episodeId, that.episodeId))
            return false;
        if (!equals(episode, that.episode)) return false;
        return equals(downloadId, that.downloadId);
    }


    private boolean equals(String s1, String s2) {
        if (s1 == null) s1 = "";
        if (s2 == null) s2 = "";
        return s1.equals(s2);
    }

    @Override
    public int hashCode() {
        int result = contentId != null ? contentId.hashCode() : 0;
        result = 31 * result + (episodeId != null ? episodeId.hashCode() : 0);
        result = 31 * result + (episode != null ? episode.hashCode() : 0);
        result = 31 * result + (downloadId != null ? downloadId.hashCode() : 0);
        result = 31 * result + downloadType;
        result = 31 * result + type;
        result = 31 * result + bmonth;
        return result;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
//        DLog.e("DownloadDBItem.writeToParcel :" + this);
        dest.writeLong(id);
        dest.writeString(title);
        dest.writeString(contentId);
        dest.writeString(episodeId);
        dest.writeString(episode);
        dest.writeString(url);
        dest.writeInt(isVip);
        dest.writeString(downloadId);
        dest.writeInt(downloadType);
        dest.writeInt(type);
        dest.writeInt(definition);
        dest.writeInt(bmonth);
        dest.writeString(urlRouter);
        dest.writeString(img);
        dest.writeString(path);
        dest.writeByte((byte) (resume ? 1 : 0));
        dest.writeByte((byte) (autoRename ? 1 : 0));
        dest.writeInt(state);
        dest.writeLong(totalSize);
        dest.writeLong(downloadSize);
        dest.writeLong(downloadSpeed);
        dest.writeInt(errorCode);
        dest.writeString(errorMsg);
        dest.writeLong(watchTime);
        dest.writeLong(createTime);
    }

    public void readFromParcel(Parcel in) {
//        DLog.e("DownloadDBItem.readFromParcel start:" + this);
        id = in.readLong();
        title = in.readString();
        contentId = in.readString();
        episodeId = in.readString();
        episode = in.readString();
        url = in.readString();
        isVip = in.readInt();
        downloadId = in.readString();
        downloadType = in.readInt();
        type = in.readInt();
        definition = in.readInt();
        bmonth = in.readInt();
        urlRouter = in.readString();
        img = in.readString();
        path = in.readString();
        resume = in.readByte() != 0;
        autoRename = in.readByte() != 0;
        state = in.readInt();
        totalSize = in.readLong();
        downloadSize = in.readLong();
        downloadSpeed = in.readLong();
        errorCode = in.readInt();
        errorMsg = in.readString();
        watchTime = in.readLong();
        createTime = in.readLong();
//        DLog.e("DownloadDBItem.readFromParcel end:" + this);
    }

    public DownloadDBItem readFrom(DownloadDBItem target) {
//        DLog.e("DownloadDBItem.readFromParcel start:" + this);
        if (target == this) {
            return this;
        }
        title = target.title;
        contentId = target.contentId;
        episodeId = target.episodeId;
        episode = target.episode;
        url = target.url;
        isVip = target.isVip;
        downloadId = target.downloadId;
        downloadType = target.downloadType;
        type = target.type;
        definition = target.definition;
        bmonth = target.bmonth;
        urlRouter = target.urlRouter;
        img = target.img;
        path = target.path;
        resume = target.resume;
        autoRename = target.autoRename;
        state = target.state;
        totalSize = target.totalSize;
        downloadSize = target.downloadSize;
        downloadSpeed = target.downloadSpeed;
        errorCode = target.errorCode;
        errorMsg = target.errorMsg;
        watchTime = target.watchTime;
        createTime = target.createTime;
//        DLog.e("DownloadDBItem.readFromParcel end:" + this);
        return this;
    }


    public static class Builder {

        private DownloadDBItem target = new DownloadDBItem();

        public Builder setTitle(String title) {
            target.setTitle(title);
            return this;
        }

        public Builder setContentId(String contentId) {
            target.setContentId(contentId);
            return this;
        }

        public Builder setEpisodeId(String episodeId) {
            target.setEpisodeId(episodeId);
            return this;
        }

        public Builder setEpisode(String episode) {
            target.setEpisode(episode);
            return this;
        }

        public Builder setUrl(String url) {
            target.setUrl(url);
            return this;
        }

        public Builder setIsVip(int isVip) {
            target.setIsVip(isVip);
            return this;
        }

        public Builder setDownloadId(String downloadId) {
            target.setDownloadId(downloadId);
            return this;
        }

        public Builder setDownloadType(int downloadType) {
            target.setDownloadType(downloadType);
            return this;
        }

        public Builder setType(int type) {
            target.setType(type);
            return this;
        }

        public Builder setDefinition(int definition) {
            target.setDefinition(definition);
            return this;
        }

        public Builder setBmonth(int bmonth) {
            target.setBmonth(bmonth);
            return this;
        }

        public Builder setUrlRouter(String urlRouter) {
            target.setUrlRouter(urlRouter);
            return this;
        }

        public Builder setImg(String img) {
            target.setImg(img);
            return this;
        }


        public Builder setResume(boolean resume) {
            target.setResume(resume);
            return this;
        }

        public Builder setAutoRename(boolean autoRename) {
            target.setAutoRename(autoRename);
            return this;
        }

        public DownloadDBItem builder() {
            return target;
        }
    }

    @Override
    public String toString() {
        return "DownloadDBItem{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", contentId='" + contentId + '\'' +
                ", episodeId='" + episodeId + '\'' +
                ", episode='" + episode + '\'' +
                ", url='" + url + '\'' +
                ", isVip=" + isVip +
                ", downloadId='" + downloadId + '\'' +
                ", downloadType=" + downloadType +
                ", type=" + type +
                ", definition=" + definition +
                ", bmonth=" + bmonth +
                ", urlRouter='" + urlRouter + '\'' +
                ", img='" + img + '\'' +
                ", path='" + path + '\'' +
                ", resume=" + resume +
                ", autoRename=" + autoRename +
                ", state=" + state +
                ", totalSize=" + totalSize +
                ", downloadSize=" + downloadSize +
                ", downloadSpeed=" + downloadSpeed +
                ", errorCode=" + errorCode +
                ", errorMsg='" + errorMsg + '\'' +
                ", watchTime=" + watchTime +
                ", createTime=" + createTime +
                '}';
    }
}
